<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录表单区域 -->
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            prefix-icon="iconfont el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 账号 -->
        <el-form-item prop="account">
          <el-input
            v-model="registerForm.account"
            prefix-icon="iconfont el-icon-user-solid"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            prefix-icon="iconfont el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 学院-教研室 -->
        <el-form-item prop="college">
          <el-select
            @change="selectChange"
            v-model="selectValues"
            placeholder="请选择学院"
          >
            <el-option
              v-for="item in collegeOption"
              :key="item.name"
              :label="item.value"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 职位 -->
        <el-form-item prop="position">
          <el-input
            v-model="registerForm.position"
            prefix-icon="iconfont el-icon-s-platform"
            placeholder="请输入职位"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
        <el-link type="info" class="linkToLogin" @click="linkToLogin"
          >已有账号？点击登录</el-link
        >
        <el-button
          class="adminBtn"
          :type="adminBtnType"
          size="mini"
          @click="handleAdminBtnClick"
          >admin</el-button
        >
      </el-form>
    </div>
    <el-dialog
      title="请输入管理秘钥"
      :visible.sync="adminDialogShow"
      width="30%"
      @close="handleAdminDialogClose"
      class="adminDialog"
    >
      <span>
        <el-input
          v-model="adminKey"
          placeholder="请输入管理员秘钥"
          size="normal"
          clearable
          type="password"
        ></el-input>
      </span>
      <span slot="footer">
        <el-button @click="adminDialogShow = false">Cancel</el-button>
        <el-button type="primary" @click="handleAmdinDialogConfirm"
          >OK</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "myLogin",
  data() {
    return {
      registerForm: {
        username: "",
        account: "",
        password: "",
        college: "",
        position: "",
        admin: false,
      },
      // 这是表单的验证规则对象
      registerFormRules: {
        username: [
          {
            required: true,
            message: "请输入登录名称",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        account: [
          {
            required: true,
            message: "请输入登录名称",
            trigger: "blur",
          },
          {
            validator: this.checkData,
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入登录密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      // 判断头像是否上传
      isDiyAvatar: false,
      collegeOption: [],
      selectValues: [],
      // admin
      adminDialogShow: false,
      adminKey: "",
      adminBtnType: "info",
    };
  },
  methods: {
    // 选择学院
    selectChange(val) {
      this.registerForm.college = val;
    },
    // 点击重置按钮，重置表单
    linkToLogin() {
      this.$router.push("/login");
    },
    async register() {
      this.$refs.registerFormRef.validate(async (vaild) => {
        if (!vaild) return;
        let option = {
          user: this.registerForm,
        };
        const { data: res } = await this.$axios.post("/register", option);

        // 用户名已注册
        if (res.status !== 200) {
          this.$message.closeAll();
          return this.$message({
            message: res.message,
            duration: 500,
          });
        }

        if (res.status == 200) {
          this.$message.success("注册成功");
          this.$router.push("/login");
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      if (res.status == 200) {
        this.$message.success("注册成功");
        return this.$router.push("/login");
      } else {
        this.$message.error("注册失败");
      }
    },
    handleChange(file) {
      this.isDiyAvatar = true;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    // 输入框不能有中文
    checkData(rule, value, callback) {
      if (value) {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          callback(this.$message.error("不能包含中文!"));
        } else {
          callback();
        }
      }
      callback();
    },
    async getCollege() {
      let { data: res } = await this.$axios.get("/college");
      if (res.status !== 200) {
        return this.$message.error("获取网络数据失败");
      }
      this.collegeOption = res.result;
    },
    // admin操作
    handleAdminBtnClick() {
      if (this.adminBtnType === "success") {
        this.adminBtnType = "info";
        return this.$message.info("out admin register");
      }
      this.adminDialogShow = !this.adminDialogShow;
    },
    async handleAmdinDialogConfirm() {
      if (this.adminKey == "") {
        return this.$message.info("please input admin key");
      }
      let { data: res } = await this.$axios.post("/admin", {
        adminKey: this.adminKey,
      });
      if (res.status !== 200) {
        this.adminBtnType = "info";
        return this.$message.error(res.message);
      }
      this.registerForm.admin = true;
      this.adminBtnType = "success";
      this.$message.success("admin register");
      this.adminDialogShow = !this.adminDialogShow;
    },
    handleAdminDialogClose() {
      this.adminKey = "";
    },
  },
  mounted() {
    this.getCollege();
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background: url(../assets/dl-login.jpg);
  background-size: 100% 100%;
  height: 100%;
}

.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 400px;
  background-color: #fff;
  border-radius: 3px;
}

.avatar_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 130px;
  height: 130px;
  border: 1px soild #eee;
  border-radius: 50%;
  padding: 10px;
  background-color: transparent;
  .avatar-uploader {
    height: 130px;
    height: 130px;
    border-radius: 50%;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;

    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 130px;
    height: 130px;
    line-height: 130px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.linkToLogin {
  position: absolute;
  left: 50%;
  bottom: 2%;
  transform: translateX(-50%);
  font-size: 10px;
}

.el-select {
  width: 100%;
}

// admin
.adminBtn {
  position: absolute;
  right: 0;
  bottom: 0;
}
.adminDialog {
  margin-top: 20vh !important;
}
</style>
